﻿using System.Security.Claims;
using Microsoft.AspNetCore.Http;

namespace FrameWork.JWT;

public interface IUserContext
{
    string UserId { get; }
    string Token { get; }
}

public class HttpUserContext : IUserContext
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public HttpUserContext(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public string Token
    {
        get
        {
            var authHeader = _httpContextAccessor.HttpContext.Request.Headers["Authorization"];
            return authHeader.Count == 0 ? "" : authHeader[0].Replace("Bearer ", "");
        }
    }

    public string UserId
    {
        get
        {
            var claim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier);
            return claim?.Value ?? "";
        }
    }
}